<?php


namespace App\Services;


use App\Models\ChatGroup;
use App\Models\ChatMessage;
use App\Models\PasswordReset;
use App\Models\User;
use App\Models\UserHasFollow;
use GatewayWorker\Lib\Gateway;

/**
 * Created By FreeAbrams
 * Date: 2021/6/17
 */
class WebSocketTokenService
{
    /**加密
     * @param array $data
     * @param $screct_key
     * @return false|string
     */
   static public function signEncrypt($data = [], $screct_key)
    {
        $str = json_encode($data);
        $res = "-----BEGIN PUBLIC KEY-----\n" .
            wordwrap($screct_key, 64, "\n", true) .
            "\n-----END PUBLIC KEY-----";
        $dcyCont = "";
        $return_en = openssl_public_encrypt($str, $dcyCont, $res);
        if(!$return_en){
            return false;
        }
        $dcyCont = base64_encode($dcyCont);
        return $dcyCont;
    }

    /**
     * 解密
     * @param $str
     * @param $screct_key
     * @return mixed
     */
    static public function signDecrypt($str, $screct_key)
    {
        $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
            wordwrap($screct_key, 64, "\n", true) .
            "\n-----END RSA PRIVATE KEY-----";
        $str = base64_decode($str);
        $dcyCont = "";
        openssl_private_decrypt($str, $dcyCont, $res);
        return json_decode($dcyCont, true);
    }
}
